package com.liuxu.autorun;

import android.graphics.Rect;
import android.os.Bundle;
import android.view.accessibility.AccessibilityNodeInfo;

import java.io.DataOutputStream;
import java.io.OutputStream;
import java.util.List;

public class utils {
    private  DataOutputStream os;
    public final void execShellCmd(String cmd) {
        try {
//            DataOutputStream dataOutputStream= new DataOutputStream(os);
            if (os==null){
                os=new DataOutputStream(Runtime.getRuntime().exec("su").getOutputStream());
            }
            os.writeBytes(cmd+"\n");
//            dataOutputStream.flush();
//            dataOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void Click(List<AccessibilityNodeInfo> Node) throws InterruptedException {
        if(Node.size()!=0) {
            Rect rect = new Rect();
            Node.get(0).getBoundsInScreen(rect);
            String x = String.valueOf((rect.left + rect.right) / 2);
            String y = String.valueOf((rect.top + rect.bottom) / 2);
            Thread.sleep(500);
            execShellCmd("input tap " + x + " " + y);
        }
        else {

        }
    }
    public void Back(List<AccessibilityNodeInfo> Node){
        if(Node.size()!=0) {
            execShellCmd("input keyevent 4");
        }
    }
    public void set_text(List<AccessibilityNodeInfo> Node,String word) throws Exception {
        if (Node.size() != 0) {
            Bundle arguments = new Bundle();
            arguments.putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, word);
            Node.get(0).performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments);
        }
        else {
            throw new Exception("找不到");
        }
    }
    public void swipe(int x1,int y1,int x2,int y2,int time){
        execShellCmd("input swipe "+x1+" "+y1+" "+x2+" "+y2+" "+time);
    }
}
